<?php
	//only takes posts. Check the posts exist. If not , return an error.
	
	include 'alarm.php';
	
	if(isset($_POST['alarmName']) && isset($_POST['alarmHours']) && isset($_POST['alarmMinutes']))
	{
		$alarmName = $_POST['alarmName'];
		$alarmHours = $_POST['alarmHours'];
		$alarmMinutes = $_POST['alarmMinutes'];
		
		$alarmTime = takeSpace($alarmHours) . takeSpace($alarmMinutes);
		
		$response = sqlAddAlarm($alarmName,$alarmTime);
		
		print $response;
		#try
		#{
		#	$output = shell_exec('crontab_edit.sh' . );
		#}
		#catch(Exception $error)
		#{
		#	echo "An error occurred while adding the alarm to crontab: " . $error;
		#}
	}
	else
	{
		return 'The appAlarm.php script is only for use while posting.';
	}
	
	function sqlAddAlarm($sqlAlarmName,$sqlAlarmTime)
	{
		if( newAlarm($sqlAlarmName,$sqlAlarmTime) == 0)
		{
			return "Added! \n\n " . $sqlAlarmName . " \n " . $sqlAlarmTime;
		}
		else
		{
			return 'The SQL INSERT failed. Adding the Alarm was not possible.';
		}
	}
	
?>